package com.usian.controller;

import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@RequestMapping("/login")
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public String login(User user, HttpSession session){
        if(user!=null &&user.getUserName()!=null &&user.getPassword()!=null){
            //校验用户登录信息
            User user1 = userService.findByUserNameAndPassword(user.getUserName(), user.getPassword());
            if(user1!=null){
                //用户信息放入session作用域
                session.setAttribute("user",user1);
                return "redirect:/user/findAll";
            }else{
                return "login";
            }
        }
        return "login";
    }
}
